var soundMgr = require('soundMgr');

let nodeMgr = require('nodeMgr');
var consts = require('consts');
var eventsMgr = require('eventsMgr');

cc.Class({
    extends: cc.Component,

    properties: {
        enableDragEvent: {
            // ATTRIBUTES:
            default:false,        // The default value will be used only when the component attaching
            serializable: true,   // optional, default is true
            tooltip:'是否监听拖拽成功事件',
        },
        enableClickEvent: {
            // ATTRIBUTES:
            default:false,        // The default value will be used only when the component attaching
            serializable: true,   // optional, default is true
            tooltip:'是否监听点击成功事件',
        },
        enablePengZhuangEvent: {
            // ATTRIBUTES:
            default:false,        // The default value will be used only when the component attaching
            serializable: true,   // optional, default is true
            tooltip:'是否监听点击成功事件',
        },
    },
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        let self = this;
        if (self.enableDragEvent){
            eventsMgr.on(consts.EVENTS.DRAG_START,self.onDragStart,self);
            eventsMgr.on(consts.EVENTS.DRAG_SUCCESS,self.onDragSuccess,self);
            eventsMgr.on(consts.EVENTS.DRAG_WRONG,self.onDragWrong,self);
        }
        cc.log('注册点击事件',self.enableClickEvent);
        if (self.enableClickEvent){
            cc.log('注册点击事件');
            
            eventsMgr.on(consts.EVENTS.CLICK,self.onClick,self);
        }
        if (self.enablePengZhuangEvent){
            cc.director.getCollisionManager().enabled = true;
            // eventsMgr.on(consts.EVENTS.HeDan,self.hedan,self);
            // eventsMgr.on(consts.EVENTS.JiaXue,self.jiaxue,self);
            // eventsMgr.on(consts.EVENTS.GuaiWu,self.guaiwu,self);
            // eventsMgr.on(consts.EVENTS.FeiJi,self.feiji,self);
            eventsMgr.on('GUAI_DEATH',self.guai_death,self);
        }
    },
    onDestroy(){
        let self = this;
        if (self.enableDragEvent){
            eventsMgr.off(consts.EVENTS.DRAG_START,self.onDragStart);
            eventsMgr.off(consts.EVENTS.DRAG_SUCCESS,self.onDragSuccess);
            eventsMgr.off(consts.EVENTS.DRAG_WRONG,self.onDragWrong);
        }
        if (self.enableClickEvent){
            eventsMgr.off(consts.EVENTS.CLICK,self.onClick);
        }
        if (self.enablePengZhuangEvent){
            cc.director.getCollisionManager().enabled = false;
            // eventsMgr.off(consts.EVENTS.HeDan,self.hedan);
            // eventsMgr.off(consts.EVENTS.JiaXue,self.jiaxue);
            // eventsMgr.off(consts.EVENTS.GuaiWu,self.guaiwu);
            // eventsMgr.off(consts.EVENTS.FeiJi,self.feiji);
            eventsMgr.off('GUAI_DEATH',self.zidan);
        }

    },
    start () {

    },


    hedan(){

    },

    jiaxue(){

    },

    guaiwu(){

    },

    feiji(){

    },

    guai_death(){

    },

    loadLevel(levelId){
        nodeMgr.showChangeScene(function () {
            let levelMgr = require('levelMgr');
            levelMgr.passLevel();
            levelMgr.loadLevel('prefabs/level'+levelId,function (err) {
                nodeMgr.hideChangeScene(function () {
                });
                levelMgr.curLevel.startLevel();
            })
        });
    },
    _gameOver(){
        this.node.destroy();
        eventsMgr.emit('game_over', {});
        soundMgr.playSoundEffectByName('tongyong_congratulations');
    },

    /**
     * 拖拽开始，一般在此步骤取消引导
     * @param data
     */
    onDragStart(data){

    },
    /**
     * 拖拽成功
     * @param data
     * {
     *     container:目标容器
     *     drag:被拖拽节点
     * }
     */
    onDragSuccess(data){

    },
    /**
     * 拖拽错误
     * @param data
     * {
     *     container:目标容器
     *     drag:被拖拽节点
     * }
     */
    onDragWrong(data){

    },
    onClick(data){
        cc.log('点击事件',data);
    }
    // update (dt) {},
});
